package routers

import (
	"log"
	"os"
	"zzxq/common/datasource"
	"zzxq/common/logger"
	"zzxq/common/middleware/cors"
	"zzxq/common/middleware/jwt"
	"zzxq/common/middleware/response"
	"zzxq/common/setting"
	"zzxq/controller"

	"github.com/gin-gonic/gin"
)

//InitRouter 初始化Router
func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(response.ResponseMiddleware())
	r.Use(gin.Logger())
	r.Use(cors.CorsHandler())
	r.Use(gin.Recovery())
	gin.SetMode(setting.Config.APP.RunMode)
	Configure(r)
	return r
}

//Configure 配置router
func Configure(r *gin.Engine) {
	//zap log init
	logger.Init()

	//database connect
	if err := datasource.RedisInit(); err != nil {
		log.Println("db fatal:", err)
		os.Exit(1)
	}

	if err := datasource.Connect(); err != nil {
		log.Println("db fatal:", err)
		os.Exit(1)
	}

	//controller declare
	var user controller.User
	var sms controller.Sms
	var tag controller.Tag
	var remind controller.Remind

	//路由列表
	r.POST("/login", user.Login)
	r.POST("/loginphone", sms.SendLoginCode)

	//获取用户相关
	userApi := r.Group("/user")
	userApi.Use(jwt.JWTAuthMiddleware())
	{
		userApi.GET("/info", user.GetUserInfo)
	}

	sysApi := r.Group("/sys")
	sysApi.Use(jwt.JWTAuthMiddleware())
	{
		sysApi.GET("/tag", tag.GetUserTags)
		sysApi.POST("/tag/create", tag.CreateUserTag)
		sysApi.GET("/remind", remind.GetDateList)
		sysApi.POST("/remind/create", remind.AddRemind)
	}


}
